﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using FolderZipper;






namespace CMD.BAL.MISC
{
    

    public class SincronizaCatalogos
    {
        
        private string[] listarArchivos(String dir, string filtro)
        {
            string[] directorios = new string[0];
            try
            {
                directorios = Directory.GetFiles(dir, filtro, SearchOption.AllDirectories);
            }
            catch (Exception)
            {
                directorios = new string[0];
            }
            return directorios;
        }
        public void ExecuteBulkInsert(string tabla,string file)
        {
            string consulta = ("TRUNCATE TABLE " + tabla + "  BULK INSERT " + tabla + " FROM '" + file + "' WITH (CodePage='ACP')");
            try
            {
                CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
            }
            catch (Exception Error)
            {
                throw (new Exception(Error.ToString()));
            }
        }
        public void ExecuteSQLUpdateCatalogos(string tabla)
        {
             string consulta = "EXEC SP_CMD_UPDATE_CATALOGOS '" + tabla +"'";
            try
            {
                CMD_DKT.DAL.SrvDB.ExecuteNonQuery(consulta);
            }
            catch (Exception Error)
            {
                throw (new Exception(Error.ToString()));
            }
            
        }
        public void InsertCMD_TMP_CAT_FILES(string FULL_FILENAME, string CATALOG_NAME, long FILENAME_DATE)
        {

            //ejecutar store
            string consulta = String.Format("EXEC SP_CMD_INSERT_TMP_CAT_FILES '{0}','{1}',{2}", FULL_FILENAME, CATALOG_NAME, FILENAME_DATE);
            CMD_DKT.DAL.SrvDB.ExecuteNonQuery(consulta);

        }
        public void InsertCMD_CURRENT_CAT_FILES(string FULL_FILENAME, string CATALOG_NAME, long FILENAME_DATE,bool STATUS)
        {

            //ejecutar store
            string consulta = String.Format("SP_CMD_INSERT_CURRENT_CAT_FILES '{0}','{1}',{2},{3}", FULL_FILENAME, CATALOG_NAME, FILENAME_DATE, STATUS);
            CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);

        }
        public void CompareCMD_TMP_CAT_FILES()
        {

            string consulta = String.Format("EXEC SP_CMD_DELETE_CMD_TMP_CAT_FILES");
            try
            {
                CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
            }
            catch (Exception Error)
            {
               
                throw (new Exception(Error.ToString()));
            }
        }
        public void GuardaVersion()
        {
            string consulta = String.Format("EXEC SP_CMD_UPDATE_CMD_SINCRONIZACION");
            try
            {
                CMD_DKT.DAL.SrvDB.ExecuteNonQuery(consulta);
            }
            catch (Exception Error)
            {

                throw (new Exception(Error.ToString()));
            }
        }
        public  DataTable SelectVersion()
        {
            DataTable dt;
            
            string consulta = "Exec SP_CMD_SELECT_CMD_SINCRONIZACION";
            try
            {
                dt = CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
                return dt;
            }
            catch (Exception Error) { throw (new Exception(Error.ToString())); }
        }
        public void TruncateCMD_TMP_CAT_FILES()
        {

            string consulta = String.Format("EXEC SP_CMD_TRUNCATE_CMD_TMP_CAT_FILES");
            try
            {
                CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
            }
            catch (Exception Error)
            {
                throw (new Exception(Error.ToString()));
            }
        }
        public DataTable SelectCMD_TMP_CAT_FILES()
        {
             DataTable dt;
             string consulta = "EXEC SP_CMD_SELECT_CMD_TMP_CAT_FILES";

            try
            {
                dt = CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
                return dt;
            }
            catch (Exception Error)
            {
                throw (new Exception(Error.ToString()));
            }
        }
        public bool ConsultaVersionArchivo(long date)
        {
            DataTable dt;
            string consulta = "EXEC SP_CMD_SELECT_CMD_VALIDA_CAT_FILES " + date;

            try
            {
                dt = CMD_DKT.DAL.SrvDB.ExecuteQuery(consulta);
                return bool.Parse(dt.Rows[0][0].ToString());
            }
            catch (Exception Error)
            {
                throw (new Exception(Error.ToString()));
            }
        }
        public bool ValidaActualizacionCatalogos(string ruta)
        {
            
            string[] archivosTerritorio = this.listarArchivos(ruta, "CAT_*.zip");

            if (archivosTerritorio.Length == 0)
                           return false;

            foreach (string file in archivosTerritorio)
            {
                try
                {
                    FileInfo fkpi = new FileInfo(file);
                    string catalogo = fkpi.Name.Substring(0, (fkpi.Name.IndexOf("_")));
                    string date = file.Substring(file.LastIndexOf("_") + 1, (file.Length - 5 - file.LastIndexOf("_")));
                    long dateLong = -1;
                    try
                    {
                        dateLong = Convert.ToInt64(date);
                    }
                    catch { }

                  

                    if (this.ConsultaVersionArchivo(dateLong) == true)
                        return true;
                }

                catch (Exception ex)
                {
                   return false;
                }
            }
            return false;
        }

    }
}
